
<title>选课设置</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>设置</cite></a>
        <a><cite>选课设置</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">选课设置</div>
                <div class="layui-card-body">
                    <div class="layui-form" lay-filter="electiveConfig">
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width: 28px; margin-left: 0px; ">开关</label>
                            <div class="layui-input-inline" style="width: 75px; float: left; margin-left: 10px;">
                                <input type="checkbox" lay-skin="switch" lay-text="开启|关闭" id="open" name="open">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width: 28px; margin-left: 0px;">刷新</label>
                            <div class="layui-input-inline" style="width: 75px; float: left; margin-left: 10px;">
                                <input type="checkbox" lay-skin="switch" lay-text="开启|关闭" id="refresh" name="refresh">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width: 28px; margin-left: 0px;">年份</label>
                            <div class="layui-input-inline" style="width: 75px; float: left; margin-left: 10px;">
                                <input type="text" id="year" name="year" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width: 28px; margin-left: 0px;">学期</label>
                            <div class="layui-input-inline" style="width: 75px; float: left; margin-left: 10px;">
                                <select id="semester" name="semester">
                                    <option value="0">0</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width: 28px; margin-left: 0px;">备注</label>
                            <div class="layui-form-mid layui-word-aux layui-input-inline" style="float: left; margin-left: 10px;">举例如下: </br>2018-0代表2018年夏季学期</br>2018-1代表2018年秋季学期</br>2018-2代表2019年春季学期</div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="float: left; margin-left: 66px;">
                                <button class="layui-btn" lay-submit lay-filter="setElectiveConfig">确认修改</button>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','laydate'], function () {
        var form = layui.form,
            $ = layui.$, laydate = layui.laydate;
        var configUrl = "/admin/config";
        var setElectiveUrl = "/admin/config/ecConfig";
        $.ajax({
            url:        configUrl,
            dataType:   'json',
            type:       'GET',
            complete    :function (result) {
                var data = result.responseJSON;
                var status = data.status;
                if (status !== 200){
                    layer.msg("请求配置数据失败！");
                    return;
                }
                data = data.data;
                var year = data.ecConfig.year;
                var semester = data.ecConfig.semester;
                var open = data.ecConfig.open;
                var refresh = data.ecConfig.refresh;
                laydate.render({
                    elem: '#year'
                    ,type: 'year'
                    ,value: year
                });
                form.val("electiveConfig", {
                    "semester": semester,
                    "open": open,
                    "refresh": refresh
                });
                form.render();
            }
        });
        form.on('submit(setElectiveConfig)', function (data) {
            layer.confirm("确认修改", function (index) {
                var load = layer.load(2);
                data = data.field;
                data.open = data.hasOwnProperty("open");
                data.refresh = data.hasOwnProperty("refresh");
                $.ajax({
                    url: setElectiveUrl,
                    dataType:   'json',
                    type:       'POST',
                    data:        data,
                    complete:   function (result) {
                        layer.close(load);
                        result = result.responseJSON;
                        var status = result.status;
                        if (status === 200) {
                            layer.msg("修改成功");
                        }else{
                            layer.msg("修改失败");
                        }
                    }
                });
                layer.close(index);
            })

        });
    });
</script>